<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
<!--  <meta http-equiv="X-UA-Compatible" content="IE=edge">-->
<!--  <meta name="viewport" content="width=device-width, initial-scale=1.0">-->
  <title>悬赏令信息管理登录入口</title>
  <link rel="shortcut icon" href="../img/title.jpg">
  <!-- 引入样式 -->
  <link rel="stylesheet" href="../plugins/element-ui/index.css" />
  <link rel="stylesheet" href="../css/common.css">
  <link rel="stylesheet" href="../css/login.css">
<!--  &lt;!&ndash; 引入样式 &ndash;&gt;-->
<!--  <link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css">-->



  <style>
    .body{
      min-width: 1366px;
    }
  </style>
</head> 

<body>
  <div class="login" id="login-app">
    <div class="login-box">
    <!--      左侧悬赏图-->
      <img src="../img/login/login-1.jpg" alt="">
    <!--    右侧表单-->
      <div class="login-form">
        <!--        表单-->
        <el-form ref="loginForm" :model="loginForm" :rules="loginRules" >

          <!--        标题图片-->
          <div class="login-form-title">
            <img src="../img/login/logo.png" style="width:198px;height:85px;" alt="" />
          </div>

        <!--          用户名-->
          <el-form-item prop="username">
            <el-input v-model="loginForm.username" type="text" auto-complete="off" placeholder="账号" maxlength="20"
              prefix-icon="iconfont icon-user">
            </el-input>
          </el-form-item>

        <!--          密码-->
          <el-form-item prop="password">
            <el-input v-model="loginForm.password" type="password" placeholder="密码" prefix-icon="iconfont icon-lock" maxlength="20"
              ></el-input>
          </el-form-item>

        <!--        登录按钮-->
          <el-form-item style="width:100%;">
            <el-button :loading="loading" class="login-btn" size="medium" type="primary" style="width:100%;"
              @click="handleLogin()">
              <span v-if="!loading">登录</span>
              <span v-else>登录中...</span>
            </el-button>
          </el-form-item>

<!--                  注册链接-->
          <el-form-item style="width:100%;">
          <a href="register.html" class="register-link">没有账号?注册一个</a>
          </el-form-item>

        </el-form>
      </div>
    </div>
  </div>

  <!-- 开发环境版本，包含了有帮助的命令行警告 -->
  <script src="../plugins/vue/vue.js"></script>
  <!-- 引入组件库 -->
  <script src="../plugins/element-ui/index.js"></script>
  <!-- 引入axios -->
  <script src="../plugins/axios/axios.min.js"></script>
  <script src="../js/request.js"></script>
  <script src="../js/validate.js"></script>
  <script src="../api/login.js"></script>
  <!-- 引入组件库 -->
<!--  <script src="https://unpkg.com/element-ui/lib/index.js"></script>-->

  <script>
    new Vue({
      el: '#login-app',
      data() {
        return {
          // 表单数据模型
          loginForm:{
            username: '',
            password: ''
          },

          // 登陆状态数据模型
          loading: false
        }
      },
      computed: {
          // 登陆规则
        loginRules() {
          // 用户名规则
          const validateUsername = (rule, value, callback) => {
            if (value.length < 8 ) {
              callback(new Error('用户名必须在8位以上'))
            } else {
              callback()
            }
          }

          // 密码规则
          const validatePassword = (rule, value, callback) => {
            if (value.length < 6) {
              callback(new Error('密码必须在6位以上'))
            } else {
              callback()
            }
          }

          return {
            'username': [{ 'validator': validateUsername, 'trigger': 'blur' }],
            'password': [{ 'validator': validatePassword, 'trigger': 'blur' }]
          }
        }
      },
      created() {
      },
      //登录方法
      methods: {
        handleLogin: async function () {
          this.$refs.loginForm.validate(async (valid) => {
            if (valid) {
              this.loading = true
              //参数处理
              const params = {
                ...this.loginForm
              }
              let res = await loginApi(params)

              if (String(res.code) == '1') {//1表示登录成功
                this.$message.success("登录成功!")
                localStorage.setItem('userInfo', JSON.stringify(res.data))
                window.location.href = 'index.html'
              } else {
                this.$message.error(res.msg)
                this.loading = false
              }
            }
          })
        },
      }
    })
  </script>
</body>

</html>
